package ferp.android.dialogs;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.ListView;
import ferp.android.R;

import java.util.List;

/**
 * User: olegs
 * Date: 30/05/12 23:28
 */
public class RadioListViewDialog extends ChoiceDialog implements ChoiceDialog.ListData.OnItemSafeClickListener
{
  public interface Listener
  {
    void onItemSelected(int requestCode, int selected);
  }

  public RadioListViewDialog(Context context, int resLayout)
  {
    super(context, resLayout);

    ld.setOnItemSafeClickListener(this);
  }

  public RadioListViewDialog(Context context, int resLayout, final Listener listener, final int requestCode)
  {
    this(context, resLayout);

    this.listener = listener;
    this.requestCode = requestCode;
  }

  public RadioListViewDialog(Context context)
  {
    this(context, R.layout.dialog_list_view);

    ld.setOnItemSafeClickListener(this);
  }

  public RadioListViewDialog(Context context, final Listener listener, final int requestCode)
  {
    this(context, R.layout.dialog_list_view);

    this.listener = listener;
    this.requestCode = requestCode;
  }

  @Override
  public void onItemClicked(int i)
  {
    listener.onItemSelected(requestCode, i);

    dismiss();
  }

  public void set(String title, List<String> options)
  {
    set(title, View.NO_ID, options, ListView.INVALID_POSITION);
  }

  public void set(String title, List<String> options, int selected)
  {
    set(title, View.NO_ID, options, selected);
  }

  public void set(String title, int resText, List<String> options)
  {
    set(title, resText, options, ListView.INVALID_POSITION);
  }

  public void set(String title, int resText, List<String> options, int selected)
  {
    this.title.setText(title);

    ld.set(resText, options, selected);
  }

  public void set(String title, String text, List<String> options, int selected)
  {
    this.title.setText(title);

    ld.set(Html.fromHtml(text), options, selected);
  }

  @Override
  protected int getMode() {return ListView.CHOICE_MODE_SINGLE;}

  @Override
  protected int getAdapterLayoutResource() {return android.R.layout.simple_list_item_single_choice;}

  @Override
  protected int getFooterVisibility() {return View.GONE;}

  protected Listener listener;
  protected int requestCode;
}
